// src/components/c-task/index.js
import {
  MineModel
} from '../../../models/mine.js'
const ModelMine = new MineModel()
import {
  AppModel
} from '../../../models/app.js'
const ModelApp = new AppModel()
const App =  getApp();

Component({
  /**
   * 组件的属性列表
   */
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    task:{
      type:Object,
      observer: function (newVal, oldVal, changedPath) {
        // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
        // 通常 newVal 就是新设置的数据， oldVal 是旧数据
      }
    },
    index:Number,
    code:String
  },

  /**
   * 组件的初始数据
   */
  data: {
    task: Object,
    gold:'',
    SignState: false
  },

  attached: function () {
    this.setData({
      task:this.properties.task
    })

  },

  /**
   * 组件的方法列表
   */
  methods: {
    tapToTask:function(event){
      // console.log(this.properties.task.id)
      this.triggerEvent('submitTask',{tid:this.properties.task.id,index:this.properties.index},{})
    },

     //签到
     sign:function() {
       //改变签到状态
       const params = {}
       ModelMine.Sign(params).then(res => {
         this.data.task.rebateStatus = 2
         this.setData({
           task: this.data.task,
           gold: res.data.gold,
           SignState: !this.data.SignState
         })
       })
     },

     //点击图片外区域隐藏签到成功信息
     clickMask() {
       this.setData({
         SignState: !this.data.SignState
       })
     },

     //绑定手机号
     getPhoneNumber: function (e) {
       console.log(e)
       const self = this
       if (e.detail.errMsg == 'getPhoneNumber:ok') {
         const params = {
           code: self.properties.code,
           encrypteData: e.detail.encryptedData,
           iv: e.detail.iv,
           regType: 3
         }
         ModelMine.wx_bind_phone(params).then(res => {
             wx.showToast({
               title: '手机号绑定成功',
               icon: 'none',
               success: function () {
                 // 绑定成功更新 App.globalData.userInfo
                 ModelApp._getVipInfo().then(res => {
                   App.globalData.userInfo = res.data.userInfo
                 })
                 self.data.task.rebateStatus = 1
                 self.setData({
                   task: self.data.task
                 })
                 self.setData({
                   ifName: false
                 })
               }
             })
           })
           .catch()
       } else {
         wx.showToast({
           title: '未授权',
           icon: 'none',
           success: function () {
             self.setData({
               ifName: false
             })
           }
         })
         return
       }
     },
     //发布买家秀
     comment: function () {
       wx.navigateTo({
         url: '/pages/account/order/list/index?index=' + 5 
       })
     }
  }
})
